package com.qlylkj.lbk.wxapi

import android.content.Context
import android.content.Intent
import androidx.lifecycle.Observer
import com.qlylkj.lbk.R
import com.qlylkj.lbk.app.BaseApplication
import com.qlylkj.lbk.base.BaseVMActivity
import com.tencent.mm.opensdk.constants.ConstantsAPI
import com.tencent.mm.opensdk.modelbase.BaseReq
import com.tencent.mm.opensdk.modelbase.BaseResp
import com.tencent.mm.opensdk.modelmsg.SendAuth
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler
import com.forest.core.etx.toast

class WXEntryActivity : BaseVMActivity<WeChatViewModel>(), IWXAPIEventHandler {

    companion object {

        fun weChatLogin(context: Context) {
            context.run {
                BaseApplication.getIWeChatApi(
                    success = {
                        if (!it.isWXAppInstalled) {
                            toast("未安装微信客户端,请安装微信客户端后再进行分享。")
                        } else {
                            val req = SendAuth.Req()
                            req.scope = WeChatConstant.REQ_SCOPE
                            req.state = WeChatConstant.REQ_STATE
                            if (!it.sendReq(req)) {
                                toast("调起微信失败")
                            }
                        }
                    },
                    failed = {
                        toast("调起微信失败")
                    }
                )
            }
        }
    }

    override fun providerVMClass() = WeChatViewModel::class.java
    override fun setLayoutId() = R.layout.activity_wechat_entry

    override fun initData() {
        //初始化api并向微信注册应用。
        BaseApplication.getIWeChatApi({
            it.handleIntent(intent, this@WXEntryActivity)
        })
        finish()
    }

    override fun initView() {

    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        setIntent(intent)
        initData()
    }

    override fun startObserve() {
        super.startObserve()
        mViewModel.apply {
            uiState.observe(this@WXEntryActivity, Observer { uiModel ->
                uiModel?.run {
                    showError?.let {
                        toast(it)
                        finish()
                    }
                    if (isFinish) {
                        finish()
                    }
                }
            })
        }
    }

    override fun onResp(resp: BaseResp) {
        if (resp.type == ConstantsAPI.COMMAND_SENDAUTH) {
            when (resp.errCode) {
                BaseResp.ErrCode.ERR_OK -> {
                    val authResp = resp as SendAuth.Resp
                    mViewModel.authorizationLogin(authResp.code)
                }
                BaseResp.ErrCode.ERR_AUTH_DENIED -> {
                    toast("拒绝授权")
                    finish()
                }
                BaseResp.ErrCode.ERR_USER_CANCEL -> {
                    toast("取消授权")
                    finish()
                }
                else -> {
                    toast("取消授权")
                    finish()
                }
            }
        } else if (resp.type == ConstantsAPI.COMMAND_SENDMESSAGE_TO_WX) {
            when (resp.errCode) {
                BaseResp.ErrCode.ERR_OK -> {
                    finish()
                }
                BaseResp.ErrCode.ERR_USER_CANCEL -> {
                    finish()
                }
                BaseResp.ErrCode.ERR_SENT_FAILED -> {
                    finish()
                }
                else -> {
                    finish()
                }
            }
        }
    }

    override fun onReq(req: BaseReq) {

    }


}